﻿$(document).ready(function () {
    //$('.task-tabs li a').click(function () {
    //    if ($(this).attr("href") == "#alltasks" && $('#ifFaqAdmin').html() == "") {
    //        FilterViewAllTask(0);
    //    }
    //    if ($(this).attr("href") == "#alltasks" && $('#ifFaqAdmin').attr("src") == "") {
    //        $('#ifFaqAdmin').load("/orderview/viewfaq");
    //    }
    //});
    $('a[href="#faqadmin"]').click(function () {
        if ($('#ifFaqAdmin').attr("src") == "") {
            $('#ifFaqAdmin').hide();
            $('#faqadmin .ajax-loading').show();
            $("#ifFaqAdmin").attr("src", "/orderview/viewfaqs");
        }
    });
    $('a[href="#alltasks"]').click(function () {
        if (!$('#LstViewAllTasks').hasClass('IsLoaded')) {
            FilterViewAllTask(0);
            $('#LstViewAllTasks').addClass('IsLoaded');
        }
    });
    $('#ifFaqAdmin').load(function () {
        $('#ifFaqAdmin').show();
        $('#faqadmin .ajax-loading').hide();
    });
});

function ViewTaskApprove(taskid) {
    $.ajax({
        url: '/task/approvetask',
        data: { id: taskid },
        success: function (data) {
            $("#ApproveTask").html(data);
            $('#datetimepicker4').datetimepicker({
                language: 'en',
                pick12HourFormat: true,
                orientation: "top left"
            });
        }
    });
}
function btnapproveclick() {
    var orderRef = $("#txtOrderRef").val();
    var reason = $("#txtreason").val();
    var title = $("#TaskTitleID").val();
    var existOrderRef;
    $(".bodyhf-ApproveTask").hide();
    $(".body-ApproveTask").append("<div class='ajax-loadForm'></div>");
    if (orderRef=="") {
        $(".ajax-loadForm").hide();
        $(".bodyhf-ApproveTask").show();
        $("#txtOrderRef").css("border-color:red");
        $("#txtOrderRef").focus();
        return;
    }
    if (reason == "") {
        $(".ajax-loadForm").hide();
        $(".bodyhf-ApproveTask").show();
        $("#txtreason").css("border-color:red");
        $("#txtreason").focus();
        return;
    }
    if (title == null) {
        $(".ajax-loadForm").hide();
        $(".bodyhf-ApproveTask").show();
        $("#TaskTitleID").css("border-color:red");
        $("#TaskTitleID").focus();
        return;
    }
    $.ajax({
        type: "POST",
        url: '/task/CheckOrderRef',
        data: { orderref: orderRef },
        dataType: "text",
        success: function (data) {
            if (data == 'True') {
                existOrderRef = true;
                $('#frApproveTask').submit();
                $("#btnApproveTask").hide();
                $("#btndeletetask").hide();
                $(".msg-ApproveTask").hide();
            }
            else {
                existOrderRef = false;
                $(".ajax-loadForm").hide();
                $(".bodyhf-ApproveTask").show();
                $(".msg-ApproveTask").show();
            }
        }
    });
};
function tasktypeapprovechange(taskTypeId) {
    $.ajax({
        url: "/task/gettitles",
        data: { taskTypeId: taskTypeId },
        success: function (data) {
            $("#TaskTitleID").html('');
            $.each(data, function (id, option) {
                $("#TaskTitleID").append($('<option></option>').val(option.id).html(option.name));
            });
        }
    }
    );
}
function sitesubmissionsfilter(companyId) {
    App.blockUI($('.most-recent-question'), false);
    $.ajax({
        url: "/task/sitesubmissionsfilter",
        data: { companyId: companyId },
        success: function (data) {
            $(".most-recent-question").html(data);
            App.unblockUI($('.most-recent-question'));
        }
    }
    );
}

function FilterViewAllTask(taskTypeId) {
    App.blockUI($("#alltasks"));
    $.ajax({
        url: "/task/GetAllTasks",
        data: { taskTypeId: taskTypeId },
        success: function (data) {
            $("#LstViewAllTasks").html(data);
            LoadDataTable($('#alltasks table'));
            App.unblockUI($("#alltasks"));
        },
        error:function(){
            App.unblockUI($("#alltasks"));
        }
    }
   );
}
function RefreshCurrentTasks(taskId) {
    App.blockUI($("#currenttasks"));
    $.ajax({
        url: "/task/currenttask",
        data:{taskId:taskId},
        success: function (data) {
            $("#currenttasks").html(data);
            LoadDataTable($('#currenttasks table'));
            App.unblockUI($("#currenttasks"));
            $('.orderdetail-task-tabs a[href="#currenttasks"]').tab('show'); // Select last tab
        },
        error: function () {
            App.unblockUI($("#currenttasks"));
        }
    }
   );
}
function ViewNewTasks() {
    App.blockUI($("#currenttasks"));
    $.ajax({
        url: "/task/OldestNewTaskId",
        success: function (data) {
            $('.orderdetail-task-tabs a[href="#currenttasks"]').tab('show'); // Select last tab
            $.ajax({
                url: "/task/currenttask",
                data: { taskId: data },
                success: function (res) {
                    $("#currenttasks").html(res);
                    var $table = $('.fixhead .table-fix');
                    $table.floatThead({
                        scrollContainer: function ($table) {
                            return $table.closest('.fixhead');
                        }
                    });
                    App.unblockUI($("#currenttasks"));
                    $('#currenttask-scroll').animate({
                        scrollTop: $("#currenttask_" + data).offset().top - $("#currenttasks").offset().top-35
                    }, 1000);
                },
                error: function () {
                    App.unblockUI($("#currenttasks"));
                }
            }
           );
        },
        error: function () {
        }
    }
   );
}
// load pending count
function initAutoRefreshStatus() {
       setInterval(
       function () {
           RefreshTaskStats();
       }, 10000); // refresh every 10000 millisecon

}
function RefreshTaskStats() {
    $.ajax({
        url: "/task/GetCountNewTask",
        success: function (data) {
            $("#CountTaskWaitting").html(data);
        },
        error: function () {
        }
    });
    $.ajax({
        url: "/task/TaskRateImage",
        success: function (data) {
            $("#TaskStatusImg").html(data);
        },
        error: function () {
        }
    });
}